%THIS CODE REPLICATE THE EXPERIMENTS OF
% F. Marra, G. Poggi, C. Sansone, L. Verdoliva
% Blind PRNU-based Image Clustering for Source Identification
% IEEE Transactions on Information Forensics and Security
% vol. 12, no. 9, pp. 2197-2211, September 2017
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% Copyright (c) 2017 Image Processing Research Group of University Federico II of Naples ('GRIP-UNINA').
% All rights reserved.
% this software should be used, reproduced and modified only for informational and nonprofit purposes.
% 
% By downloading and/or using any of these files, you implicitly agree to all the
% terms of the license, as specified in the document LICENSE.txt
% (included in this package) and online at
% http://www.grip.unina.it/download/LICENSE_OPEN.txt
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all;
close all;
fprintf('This code replicate some of the experiments in "Blind PRNU-based Image Clustering for Source Identification"\n\n');

addpath( genpath('Utility'), '-end' );
addpath( genpath('Clustering'), '-end' );

ds_root = '';

if strcmp(ds_root,'')
   warning('Please specify the path for the Dresden image database'); 
end

sets = {
    'A.1';
%    'A.2' ;
%    'A.max';
};

%% Residual Inner Product Matrix (C) Parameters
denoising_function = 'bm3d'; %'none';
crop_size = [Inf,Inf]; %[512,512];
crop_location = 'center'; %'upper-left'; 
in_memory = false; %true


%% Two step ensemble Parameters
method_ensemble = 'WEAC-SL';
use_parfor = true;              %Enable the parallel computing
verbose = true;                 %Display information
showCluster = true;             %Show the results in a graphic
showallclust = true;            %Show even the singleton cluster
putLegend = true;               %Show the legend on the graphic
%---------------------

for iset = 1:size(sets,1)
    exp_name = sets{iset,1};
    fprintf('Set: %s \n',exp_name);
    
    %% Preparing Experiments and Variables
    load(['./Data/',exp_name,'.mat'],'images_name','cameras','gtruth');
    
    % the mat-file contains:
    %
    % cameras      : a cell array with the name of the M cameras of the
    %                ground truth
    %
    % images_name  : the path of the N images used
    %
    % gtruth       : column vector with the ground truth labels
    
    images_name = fullfile(ds_root,images_name);
    
    disp('Calculating Distances...');
    C = ResidualInnerProductMatrix(images_name,denoising_function,crop_size,crop_location,in_memory);
    disp('Clustering data...');
    Index = TwoStepEnsembleClustering(C,method_ensemble,use_parfor,verbose);
    
    [ari_2step,tpr_2step,fpr_2step ] = performanceIndexs(Index,gtruth);

    fprintf('Clustering Performance------\n');
    fprintf('ARI\t=\t %.3f\n',ari_2step);
    fprintf('TPR\t=\t %.2f%%\n',tpr_2step*100);
    fprintf('FPR\t=\t %.2f%%\n',fpr_2step*100);
    fprintf('N \t=\t %d\n',numel(unique(Index)));
    fprintf('----------------------------\n')
    
    if showCluster
        title = ['Set ',exp_name,' - P = ',num2str((ari_2step),4),' - Nclust = ',num2str(numel(unique(Index))) ];
        h = clusterView(Index,gtruth,cameras,gtruth,title,showallclust,true);
    end
end
